package attackDetection;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.sql.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author john
 */
public class Nstat {

    private ArrayList<Double> hoptime;
    private int nohop;
    private double meanhoptime;
    private double sdhoptime;
    private ArrayList<Node> nodelist;
    private double meanmsg;
    private double sdmsg;

    public Nstat() {
        hoptime = new ArrayList<Double>();
        nodelist = new ArrayList<Node>();
        meanhoptime = 0.0;
        sdhoptime = 0.0;
        meanmsg = 0.0;
        sdmsg = 0.0;
    }

    public void loadhoptime() {
        Storage.DBConnect db = new Storage.DBConnect(), db2 = new Storage.DBConnect();
        ResultSet results, results2;
        Plog cplog, nplog;
        int i = 0;
        double temp = 0.0;
        double diff1 = 0.0;
        int rowCount = 0;

        cplog = new Plog();
        nplog = new Plog();

        db.connectToDatabase();
        db2.connectToDatabase();
        db.executeQuery("SELECT * FROM log WHERE opmode!='s'", true);
        results = db.getResultSet();

        try {
            results.last();
            results.first();
        } catch (Exception e) {
        }

        try {
            if (!results.first()) {
                System.out.println("Sql Error");
            }

            do {

                cplog.Init(results);
                db2.executeQuery("SELECT * FROM log WHERE pseqno=" + cplog.getPseqno()
                        + " AND sip=" + cplog.getSip()
                        + " AND dip=" + cplog.getDip()
                        + " AND nodeip=" + cplog.getPhip(), true);

                results2 = db2.getResultSet();

                results2.last();
                rowCount = results2.getRow();
                results2.first();

                if (rowCount == 1) {
                    nplog.Init(results2);
                    diff1 = cplog.getTimeStamp().getTime() - nplog.getTimeStamp().getTime();
                }
                hoptime.add(diff1);
                i++;
            } while (results.next());
        } catch (SQLException e) {
            System.out.println("Error Message = " + e.getMessage());
        }
        nohop = i;

        /* Closing Database Connections */
        db.closeConnections();
        db2.closeConnections();

    }

    public void calHopParameter() {
        int i = 0;
        int count = 0;
        double temp = 0.0;
        double temp2 = 0.0;
        double s_dev = 0.0;

        for (i = 0; i < hoptime.size(); i++) {
            temp += hoptime.get(i);
        }
        count = i;

        setMeanhoptime(temp / count);

        temp = 0.0;

        for (i = 0; i < hoptime.size(); i++) {
            temp2 += Math.pow(getHoptime().get(i) - getMeanhoptime(), 2.0);
        }
        setSdhoptime(Math.sqrt((1.0 / count) * temp2));
    }

    /**
     * There's a file that's being read in but I'm not quite sure how that
     * file is created. This method only partially implemented
     * 
     */
    public void loadNodeList() {
        Storage.DBConnect db = new Storage.DBConnect();
        HashMap<Integer, Integer> talkativeNodes = new HashMap<Integer, Integer>();
        ResultSet results;

        BufferedReader bReader;
        Node cnode;
        Plog cplog;
        int i = 0;
        int mytemp;
        double temp = 0.0;
        double diff1 = 0.0;

        db.connectToDatabase();
        db.executeQuery("SELECT nodeid,nodeip, COUNT(plogid) FROM log where opmode = 's'  group by nodeid ", true);
        System.out.println(db.getErrorMessage());
        results = db.getResultSet();

        try {
            results.last();
            mytemp = results.getRow();
            results.first();
            System.out.println("Matched Log: " + mytemp);

            do {
                cnode = new Node();
                cnode.setNodeIP(results.getInt("nodeip"));
                cnode.setNomsg(results.getInt(2));
                cnode.setNodeId(results.getInt("nodeid"));
                
                nodelist.add(cnode);
                i++;
                
            } while (results.next());

        } catch (Exception e) {
            System.out.println("Error Message in LoadNodeList: " + e.getMessage());
        }



        try {
            bReader = new BufferedReader(new FileReader("TalkativeNodes.txt"));
        } catch (FileNotFoundException e) {
        }

        db.closeConnections();

    }

    public void calNodeParameter() {
        int i = 0;
        int count = 0;
        double temp = 0.0;
        double temp2 = 0.0;
        double s_dev = 0.0;

        for (i = 0; i < getNodelist().size(); i++) {
            temp += (getNodelist().get(i).getNomsg() / getNodelist().get(i).getTalkfactor());
        }
        count = i;

        setMeanmsg(temp / count);

        temp = 0.0;

        for (i = 0; i < getNodelist().size(); i++) {
            temp2 += Math.pow(getNodelist().get(i).getNomsg() / getNodelist().get(i).getTalkfactor() - getMeanmsg(), 2.0);
        }
        setSdmsg(Math.sqrt((1.0 / count) / temp2));

    }

    public void floodingCheck() {
        Suspicious output = new Suspicious();

        for (int i = 0; i < getNodelist().size(); i++) {
            if ((getNodelist().get(i).getNomsg() / getNodelist().get(i).getTalkfactor() > getMeanmsg() + 2 * getSdmsg())) {
                System.out.println("Flooding is found from Node: " + getNodelist().get(i).getNodeId());
                output.increase(getNodelist().get(i).getNodeId(), "flood");
            }
        }
    }

    /**
     * @return the hoptime
     */
    public ArrayList<Double> getHoptime() {
        return hoptime;
    }

    /**
     * @param hoptime the hoptime to set
     */
    public void setHoptime(ArrayList<Double> hoptime) {
        this.hoptime = hoptime;
    }

    /**
     * @return the nohop
     */
    public int getNohop() {
        return nohop;
    }

    /**
     * @param nohop the nohop to set
     */
    public void setNohop(int nohop) {
        this.nohop = nohop;
    }

    /**
     * @return the meanhoptime
     */
    public double getMeanhoptime() {
        return meanhoptime;
    }

    /**
     * @param meanhoptime the meanhoptime to set
     */
    public void setMeanhoptime(double meanhoptime) {
        this.meanhoptime = meanhoptime;
    }

    /**
     * @return the sdhoptime
     */
    public double getSdhoptime() {
        return sdhoptime;
    }

    /**
     * @param sdhoptime the sdhoptime to set
     */
    public void setSdhoptime(double sdhoptime) {
        this.sdhoptime = sdhoptime;
    }

    /**
     * @return the nodelist
     */
    public ArrayList<Node> getNodelist() {
        return nodelist;
    }

    /**
     * @param nodelist the nodelist to set
     */
    public void setNodelist(ArrayList<Node> nodelist) {
        this.nodelist = nodelist;
    }

    /**
     * @return the meanmsg
     */
    public double getMeanmsg() {
        return meanmsg;
    }

    /**
     * @param meanmsg the meanmsg to set
     */
    public void setMeanmsg(double meanmsg) {
        this.meanmsg = meanmsg;
    }

    /**
     * @return the sdmsg
     */
    public double getSdmsg() {
        return sdmsg;
    }

    /**
     * @param sdmsg the sdmsg to set
     */
    public void setSdmsg(double sdmsg) {
        this.sdmsg = sdmsg;
    }
}
